import 'antd/dist/antd.css'
import './yvan-ui.css'
import * as mobx from "mobx"
import lodash from 'lodash'
import React from 'react'
import * as ReactRouterDOM from 'react-router-dom'
import * as Antd from 'antd'
import * as ReactDOM from 'react-dom'
import * as AntdIcon from '@ant-design/icons'
import * as history from 'history'
import * as axios from 'axios'
import _ from "lodash";
import {colSetting as DefaultColSetting} from './filter/rowcols'

export {
    axios,
    history,
    mobx,
    React,
    ReactDOM,
    ReactRouterDOM,
    lodash,
    Antd,
    AntdIcon,
    DefaultColSetting
}

export {observer, Observer} from 'mobx-react'
export * from './components/index'
export * from './renderers/index'
export * from './types'
export * from './ajax'
export * from './service'
export * from './scope'
export * from './extend'
export * from './factory'
export * from './datasource'
export * from './utils/helper'
export * from './xlsx'
export * from './ajaxMethod'

export const version = "0.0.1"

/**
 * 将任意双向绑定对象转换为普通 JS 对象
 */
export const toJS = mobx.toJS

/**
 * 一次性提交修改（期间不会触发 autorun 和任何 watch 事件）
 */
export const transaction = mobx.transaction

/**
 * 创建一个接受双向绑定，或双向绑定的壳
 */
export const $$ = mobx.observable

/**
 * 解决ag-grid 复制表格内容到excel时的排版问题
 */
document.oncopy = ()=>{
    let tempNode:any = getSelection()!.getRangeAt(0).commonAncestorContainer
    let isCopyGrid = false;
    while(tempNode && tempNode != document.body && (tempNode.nodeName == 'DIV' || tempNode.nodeName == 'div')) {
        if ( tempNode.getAttribute('copytag') == 'ag-grid') {
            isCopyGrid = true;
            break;
        }
        tempNode = tempNode.parentNode;
    }
    if (isCopyGrid) {
        const copyNodes = getSelection()!.getRangeAt(0).cloneContents()

        let newTable:any = document.createElement('table');
        newTable.id = _.uniqueId('copyTable_')
        newTable.style.position='absolute';
        newTable.style.left='-999999px';
        document.body.appendChild(newTable);

        let newRow:any = document.createElement("tr");
        newRow.id = _.uniqueId('row_')
        newTable.appendChild(newRow);

        const newTableId = newTable.id;
        const newRowId = newRow.id
        makeCopyTable(copyNodes, newTableId, newRowId);
        newTable = document.getElementById(newTableId);
        getSelection()!.selectAllChildren(newTable);
        window.setTimeout(function() {
            document.body.removeChild(newTable);
        },0);
    }

}

function makeCopyTable(copyNodes: any, copyTableId: any, rowId: any) {
    let newRow = document.getElementById(rowId);
    const newTable = document.getElementById(copyTableId);
    for (let i = 0; i < copyNodes.childNodes.length; i++) {
        const node = copyNodes.childNodes[i];
        if (node.nodeName == 'DIV' || node.nodeName == 'div'){
            // @ts-ignore
            if (node.getAttribute("role") == 'gridcell') {
                const newtd = document.createElement('td');
                // @ts-ignore
                newtd.innerHTML = node.innerHTML;
                newRow!.appendChild(newtd);

                // @ts-ignore
            } else if (node.getAttribute("role") == 'row') {
                if (newRow) {
                    newRow.remove();
                    newRow = null;
                }
                const row = document.createElement("tr");
                newTable!.appendChild(row);
                for (let j = 0; j < node.childNodes.length; j++) {
                    const td = node.childNodes[j];
                    const newtd = document.createElement('td');
                    // @ts-ignore
                    newtd.innerHTML = td.innerHTML;
                    row.appendChild(newtd);
                }
            } else if (node.getAttribute("role") == 'presentation' || node.getAttribute("role") == 'rowgroup') {
                makeCopyTable(node, copyTableId, rowId);
            }
        }

    }
}